package demoMod.invincibleOne.patches;

import basemod.ReflectionHacks;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireInsertPatch;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.TipHelper;
import com.megacrit.cardcrawl.helpers.controller.CInputActionSet;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.screens.charSelect.CharacterSelectScreen;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.characters.InvincibleOne;
import demoMod.invincibleOne.skins.PrimeSkin;
import demoMod.invincibleOne.skins.Skin;

public class CharacterSelectScreenPatch {
    @SpirePatch(
            clz = CharacterSelectScreen.class,
            method = "update"
    )
    public static class PatchUpdate {
        public static void Postfix(CharacterSelectScreen screen) {
            if (screen.options.stream().anyMatch(option -> option.selected && option.c instanceof InvincibleOne)) {
                screen.isAscensionMode = false;
                ReflectionHacks.setPrivate(screen, CharacterSelectScreen.class, "isAscensionModeUnlocked", true);
                screen.ascensionLevel = 0;
            }
        }
    }

    @SpirePatch(
            clz = CharacterSelectScreen.class,
            method = "renderAscensionMode"
    )
    public static class PatchRenderAscensionMode {
        private static final float ASC_LEFT_W = FontHelper.getSmartWidth(FontHelper.cardTitleFont, CharacterSelectScreen.TEXT[6], 9999.0F, 0.0F);
        private static final float ASC_RIGHT_W = FontHelper.getSmartWidth(FontHelper.cardTitleFont, CharacterSelectScreen.TEXT[7] + "22", 9999.0F, 0.0F);

        public static SpireReturn<Void> Prefix(CharacterSelectScreen screen, SpriteBatch sb) {
            if (screen.options.stream().anyMatch(option -> option.selected && option.c instanceof InvincibleOne)) {
                Hitbox ascensionModeHb = ReflectionHacks.getPrivate(screen, CharacterSelectScreen.class, "ascensionModeHb");
                Hitbox ascLeftHb = ReflectionHacks.getPrivate(screen, CharacterSelectScreen.class, "ascLeftHb");
                Hitbox ascRightHb = ReflectionHacks.getPrivate(screen, CharacterSelectScreen.class, "ascRightHb");
                sb.draw(ImageMaster.OPTION_TOGGLE, (float) Settings.WIDTH / 2.0F - ASC_LEFT_W - 16.0F - 30.0F * Settings.scale, ascensionModeHb.cY - 16.0F, 16.0F, 16.0F, 32.0F, 32.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 32, 32, false, false);

                if (ascensionModeHb.hovered) {
                    FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, CharacterOptionPatch.TEXT[11], (float)Settings.WIDTH / 2.0F - ASC_LEFT_W / 2.0F, ascensionModeHb.cY, Settings.GREEN_TEXT_COLOR);
                    TipHelper.renderGenericTip((float) InputHelper.mX - 140.0F * Settings.scale, (float)InputHelper.mY + 340.0F * Settings.scale, CharacterOptionPatch.TEXT[11], CharacterOptionPatch.TEXT[13]);
                } else {
                    FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, CharacterOptionPatch.TEXT[11], (float)Settings.WIDTH / 2.0F - ASC_LEFT_W / 2.0F, ascensionModeHb.cY, Settings.GOLD_COLOR);
                }

                if (Skin.skinNow().isPrimeForm && InvincibleOneMod.reskinUnlocked[CardCrawlGame.saveSlot]) {
                    FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, "PRIME",
                            (float)Settings.WIDTH / 2.0F + ASC_RIGHT_W / 2.0F + 200.0F * Settings.scale,
                            ascensionModeHb.cY, Settings.BLUE_TEXT_COLOR);
                } else {
                    FontHelper.renderFontCentered(sb, FontHelper.cardTitleFont, String.format("%d%%",
                            InvincibleOneMod.seriousLevel[CardCrawlGame.saveSlot] * 10),
                            (float)Settings.WIDTH / 2.0F + ASC_RIGHT_W / 2.0F + 200.0F * Settings.scale,
                            ascensionModeHb.cY, Settings.BLUE_TEXT_COLOR);
                }
                sb.setColor(Color.WHITE);
                if (!Settings.isMobile) {
                    sb.draw(ImageMaster.OPTION_TOGGLE_ON, (float)Settings.WIDTH / 2.0F - ASC_LEFT_W - 16.0F - 30.0F * Settings.scale, ascensionModeHb.cY - 16.0F, 16.0F, 16.0F, 32.0F, 32.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 32, 32, false, false);
                } else {
                    sb.draw(ImageMaster.TICK, (float)Settings.WIDTH / 2.0F - ASC_LEFT_W - 36.0F * Settings.scale - 32.0F, ascensionModeHb.cY - 32.0F, 32.0F, 32.0F, 64.0F, 64.0F, Settings.scale * 0.9F, Settings.scale * 0.9F, 0.0F, 0, 0, 64, 64, false, false);
                }

                if (Settings.isMobile) {
                    FontHelper.renderFontCentered(sb, FontHelper.smallDialogOptionFont, screen.ascLevelInfoString, (float)Settings.WIDTH / 2.0F, 60.0F * Settings.scale, Settings.CREAM_COLOR);
                } else {
                    FontHelper.renderFontCentered(sb, FontHelper.cardDescFont_N, screen.ascLevelInfoString, (float)Settings.WIDTH / 2.0F, 35.0F * Settings.scale, Settings.CREAM_COLOR);
                }

                if (!ascLeftHb.hovered && !Settings.isControllerMode) {
                    sb.setColor(Color.LIGHT_GRAY);
                } else {
                    sb.setColor(Color.WHITE);
                }

                sb.draw(ImageMaster.CF_LEFT_ARROW, ascLeftHb.cX - 24.0F, ascLeftHb.cY - 24.0F, 24.0F, 24.0F, 48.0F, 48.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 48, 48, false, false);
                if (!ascRightHb.hovered && !Settings.isControllerMode) {
                    sb.setColor(Color.LIGHT_GRAY);
                } else {
                    sb.setColor(Color.WHITE);
                }

                sb.draw(ImageMaster.CF_RIGHT_ARROW, ascRightHb.cX - 24.0F, ascRightHb.cY - 24.0F, 24.0F, 24.0F, 48.0F, 48.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 48, 48, false, false);
                if (Settings.isControllerMode) {
                    sb.draw(CInputActionSet.proceed.getKeyImg(), ascensionModeHb.cX - 100.0F * Settings.scale - 32.0F, ascensionModeHb.cY - 32.0F, 32.0F, 32.0F, 64.0F, 64.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 64, 64, false, false);
                    sb.draw(CInputActionSet.pageLeftViewDeck.getKeyImg(), ascLeftHb.cX - 60.0F * Settings.scale - 32.0F, ascLeftHb.cY - 32.0F, 32.0F, 32.0F, 64.0F, 64.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 64, 64, false, false);
                    sb.draw(CInputActionSet.pageRightViewExhaust.getKeyImg(), ascRightHb.cX + 60.0F * Settings.scale - 32.0F, ascRightHb.cY - 32.0F, 32.0F, 32.0F, 64.0F, 64.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 64, 64, false, false);
                }

                ascensionModeHb.render(sb);
                ascLeftHb.render(sb);
                ascRightHb.render(sb);
                return SpireReturn.Return(null);
            }
            return SpireReturn.Continue();
        }
    }
}
